//
//  main.m
//  07_HomeWork
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 LST. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Pro.h"
#import "City.h"
#import "Zone.h"
int main(int argc, const char * argv[]) {
   
 
    NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/07_HomeWork/07_HomeWork/area.txt";
    NSString *areaString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [areaString componentsSeparatedByString:@"\n"];
    NSMutableArray *allArray = [NSMutableArray array];
    for (NSString *str in strArr) {
        if (![str hasPrefix:@" "]) {
            Pro *pro = [Pro zoneWithName:str number:nil];
            [allArray addObject:pro];
        } else if ([str hasPrefix:@"  "] && ![str hasPrefix:@"    "]) {
            Pro *pro = [allArray lastObject];
            City *city = [City zoneWithName:str number:nil];
            [pro.array addObject:city];
        } else {
            City *city = [[[allArray lastObject] array ]lastObject];
            Zone *zone = [Zone zoneWithName:str number:nil];
            [city.array addObject:zone];
        }
    }
    
   // NSLog(@"%@", strArr);
   // NSLog(@"%ld", strArr.retainCount);
    NSString *temp = @"/Users/dllo/DLS160710/Objective-C/HomeWork/07_HomeWork/07_HomeWork/Location1.txt";
    NSString *tempString = [NSString stringWithContentsOfFile:temp encoding:NSUTF8StringEncoding error:nil];
    NSArray *cityArr = [tempString componentsSeparatedByString:@","];
    NSMutableArray *allTempCity = [NSMutableArray array];
    for (NSString *cityString in cityArr) {
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        CityTemp *tempCity = [[CityTemp alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCity addObject:tempCity];
    }
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCity sortUsingDescriptors:@[sortTempCity]];
   // NSLog(@"%@",allTempCity);
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (CityTemp *tempCity in allTempCity) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Pro *pro = [Pro zoneWithCity:(tempCity)];
            [finalClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
            Pro *pro = [finalClassArray lastObject];
            City *city = [City zoneWithCity:tempCity];
            [pro.array addObject:city];
        } else {
            Pro *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithCity:tempCity];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", finalClassArray);
    return 0;
}
